using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;
using Seems.Model;
using Seems.View.Controls;
using Attribute = Seems.Model.Attribute;

namespace Seems.SL.VM
{
	public class DiagramVM : BasicVM, IScaleProvider
	{
		private readonly Diagram model;

		public Size ContourSize { private get; set; }

		public double Scale { get; set; }
		public double ScaleMin { get; private set; }
		public double ScaleMax { get; private set; }
		public double ScaleSmallChange { get; private set; }
		public double ScaleLargeChange { get; private set; }

		public DiagramVM(Diagram model)
		{
			this.model = model;
			ScaleMin = 0.1;
			ScaleMax = 1.0;
			ScaleSmallChange = 0.1;
			ScaleLargeChange = 0.2;
			Scale = 1;
			ShownElements = new ObservableCollection<ElementShownOnDiagramVM>();
			foreach (var element in model.Elements)
			{
				ShownElements.Add(new ElementShownOnDiagramVM(element)); 
			}
		}

		public ICommand AddElementCmd { get { return MakeCommand(AddElement); } }
		public ICommand ZoomToFitCommand { get { return MakeCommand(ZoomToFit); } }

		private void ZoomToFit()
		{
		}

		private void AddElement()
		{
			var startTime = DateTime.Now;
			const int count = 10;
			for (int i = 0; i < count; i++ )
			{
				var r = new Random();
				var elem = new DiagramElement
					{
						Element = new Element
							{
								Name = "Elem" + r.Next(100),
								Attributes = new[]
									{
										new Attribute {Name = "Attribute1"},
										new Attribute {Name = "Attribute2"},
										new Attribute {Name = "Attribute3"}
									}
							},
						Bounds = new Rect(r.Next(500), r.Next(300), 100, 100)
					};
				ShownElements.Add(new ElementShownOnDiagramVM(elem));
			}
			var perElem = (DateTime.Now - startTime).TotalMilliseconds / count;
			Console.WriteLine("ms per elem: " + perElem);
		}

		public Diagram Model
		{
			get {
				return model;
			}
		}

		public ObservableCollection<ElementShownOnDiagramVM> ShownElements { get; private set; }
	}
}